package ca.seia.comp6521.a1.sortalgorithms;


public class SelectionSort extends SortAlgorithm {

	
	public SelectionSort() {
		this.algorithmName = "Selection Sort";
	}
	
	@Override
	public void sort(int[] items) {
		
		this.numberOfInnerLoops = 0; // Statistics Code
		this.numberOfOuterLoops = 0; // Statistics Code
		this.numberOfSwaps = 0; // Statistics Code
		
//		int[] sortedItems = Arrays.copyOf(items, items.length);
		int minIndex = 0;
		int innerLoopIndex = 0;
		int outerLoopIndex = 0;
		
		long startTime = System.nanoTime(); // Statistics Code 
		
		for (outerLoopIndex = 0 ; outerLoopIndex < items.length - 1 ; outerLoopIndex++) {
			++(this.numberOfOuterLoops); // Statistics Code
			minIndex = outerLoopIndex;
			
			for( innerLoopIndex = outerLoopIndex + 1; innerLoopIndex < items.length; innerLoopIndex++) {
				++(this.numberOfInnerLoops);
				if (items[innerLoopIndex] < items[minIndex]) {
					minIndex = innerLoopIndex;
				}
			}
			
			//we have to swap the min index
			if ( minIndex != outerLoopIndex) {
				++ (this.numberOfSwaps); // Statistics Code
				int tempValue = items[outerLoopIndex];
				items[outerLoopIndex] = items[minIndex];
				items[minIndex] = tempValue;
			}
		}
		
		long endTime = System.nanoTime(); // Statistics Code
		this.executionTime = endTime - startTime;
	}

}
